package com.contract.commonlib.view

import com.contract.commonlib.util.ToolUtils.setDrawbleTint
import android.widget.LinearLayout
import android.widget.TextView
import android.view.ViewGroup
import android.widget.FrameLayout
import android.content.res.TypedArray
import com.contract.commonlib.R
import android.graphics.Typeface
import android.text.TextUtils
import android.view.LayoutInflater
import com.contract.commonlib.view.TitleBar.TitleButtonOnClickListener
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import com.contract.commonlib.util.ToolUtils

/**
 * 标题文件 属性：titleLeftArrow:boolean 返回箭头是否显示，默认显示 titleLeftText:String 左边按钮内容
 * titleName:String 标题名字 titleRight：String 右侧按钮名字
 *
 *
 * 如果需要隐藏左边窗口，需要把titleLeftArrow设置为false，并把titleLeftText设置为"".
 *
 *
 * titleOnClickListener:按钮点击事件。
 *
 *
 * 1.设置左边back 箭头，则显示返回
 *
 *
 * 2.不设置左边箭头，则显示取消
 *
 *
 * 3.可自定义标题
 *
 *
 * 4.可自定义左边按钮文字
 *
 *
 * 5.可自定义右边按钮文字（确定、发送、取消等）
 *
 * @author Administrator
 */
class TitleBar : LinearLayout {
    private var iv_back: ImageView? = null
    private var titleName: TextView? = null
    private var title_right_btn: TextView? = null
    private var titleView: ViewGroup? = null
    private var fl_customRightView: FrameLayout? = null

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        initView(context)
        val attrArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarAttr)
        val color = attrArray.getColor(R.styleable.TitleBarAttr_titleBgColor, 0)
        val titleColor = attrArray.getColor(R.styleable.TitleBarAttr_titleColor, 0)
        val titleRightColor = attrArray.getColor(R.styleable.TitleBarAttr_titleRightColor, 0)
        val aBoolean = attrArray.getBoolean(R.styleable.TitleBarAttr_titleNameIsbold, true)
        if (aBoolean) {
            titleName!!.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        } else {
            titleName!!.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        }
        val rightIsbold = attrArray.getBoolean(R.styleable.TitleBarAttr_titleRightNameIsbold, true)
        if (rightIsbold) {
            title_right_btn!!.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        } else {
            title_right_btn!!.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        }
        if (titleRightColor != 0) {
            title_right_btn!!.setTextColor(titleRightColor)
        }
        if (titleColor != 0) {
            titleName!!.setTextColor(titleColor)
        }
        if (color != 0) {
            titleView!!.setBackgroundColor(color)
        }
        // 设置左边返回按钮上的文字以及箭头是否显示。默认箭头显示，文字为返回
        val leftDrawable = attrArray.getDrawable(R.styleable.TitleBarAttr_titleLeftIco)
        if (leftDrawable != null) {
            iv_back!!.setImageDrawable(leftDrawable)
        }
        if (attrArray.getBoolean(R.styleable.TitleBarAttr_titleShowBackButton, true)) {
            iv_back!!.visibility = VISIBLE
        } else {
            iv_back!!.visibility = GONE
        }

        // 设置titleName
        val title = attrArray.getString(R.styleable.TitleBarAttr_titleName)
        if (!TextUtils.isEmpty(title)) {
            titleName!!.text = title
        }
        // 设置右边文字的内容
        val rightText = attrArray.getString(R.styleable.TitleBarAttr_titleRightName)
        val rightDrawable = attrArray.getDrawable(R.styleable.TitleBarAttr_titleRightIco)
        if (rightDrawable != null) {
            title_right_btn!!.setCompoundDrawablesWithIntrinsicBounds(
                rightDrawable,
                null,
                null,
                null
            )
        }
        if (!TextUtils.isEmpty(rightText)) {
            title_right_btn!!.text = rightText
        }
        title_right_btn!!.setOnClickListener(listener)
        iv_back!!.setOnClickListener(listener)
        attrArray.recycle()
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(R.layout.title_bar, this, true)
        iv_back = findViewById(R.id.iv_back)
        titleName = findViewById(R.id.titleName)
        title_right_btn = findViewById(R.id.title_right_btn)
        titleView = findViewById(R.id.titleView)
        fl_customRightView = findViewById(R.id.fl_customRightView)
    }

    fun setCustomRightView(layoutId: Int) {
        val view = LayoutInflater.from(context).inflate(layoutId, null)
        fl_customRightView!!.addView(view)
        view.setOnClickListener { view1: View? -> titleBtnClickListener!!.RightButtonOnClick(view1) }
    }

    var titleBtnClickListener: TitleButtonOnClickListener? = object : TitleButtonOnClickListener {
        override fun RightButtonOnClick(v: View?) {}
        override fun LeftButtonOnClick(v: View?) {
            if (context is Activity) {
                (context as Activity).finish()
            }
        }
    }
    var listener = OnClickListener { v ->
        val i = v.id
        if (i == R.id.iv_back) {
            if (titleBtnClickListener != null) {
                titleBtnClickListener!!.LeftButtonOnClick(v)
            }
        } else if (i == R.id.title_right_btn) {
            if (titleBtnClickListener != null) {
                title_right_btn!!.isEnabled = false
                titleBtnClickListener!!.RightButtonOnClick(v)
                title_right_btn!!.isEnabled = true
            }
        }
    }

    /**
     * 标题中的按钮被点击监听
     *
     * @author Administrator
     */
    interface TitleButtonOnClickListener {
        /**
         * 左边按钮被点击
         *
         * @param v
         */
        fun LeftButtonOnClick(v: View?)

        /**
         * 右边按钮被点击
         *
         * @param v
         */
        fun RightButtonOnClick(v: View?)
    }

    /**
     * 设置标题按钮被点击的事件
     *
     * @param titleBtnClickListener
     */
    fun setTitleButtonOnClickListener(titleBtnClickListener: TitleButtonOnClickListener?) {
        this.titleBtnClickListener = titleBtnClickListener
    }

    /**
     * 设置titleName
     *
     * @param title
     */
    fun setTitleName(title: String?) {
        titleName!!.text = title
    }

    fun setTitleNameColor(colorId: Int) {
        titleName!!.setTextColor(resources.getColor(colorId))
    }

    fun setTitleAlphaColor(colorId: Int) {
        titleName!!.setTextColor(colorId)
        title_right_btn!!.setTextColor(colorId)
    }

    fun setTitleDrawbleTintColor(color: Int) {
        setDrawbleTint(context, R.mipmap.app_arrow_right_white, color, iv_back!!)
    }

    /**
     * 设置右边按钮内容
     *
     * @param title
     */
    fun setTitleRightText(title: String?) {
        if (TextUtils.isEmpty(title)) {
            title_right_btn!!.visibility = GONE
        } else {
            title_right_btn!!.visibility = VISIBLE
            title_right_btn!!.text = title
        }

    }

    fun setRightdrawble(drawbleId: Int) {
        val rightDrawable = resources.getDrawable(drawbleId)
        title_right_btn!!.setCompoundDrawablesWithIntrinsicBounds(rightDrawable, null, null, null)
    }

    fun setRightVisible(visible: Int) {
        title_right_btn!!.visibility = visible
    }

    fun setLeftrdrawble(drawbleId: Int) {
        val leftDrawable = resources.getDrawable(drawbleId)
        iv_back!!.setImageDrawable(leftDrawable)
    }

    fun setShowBack(visible: Int) {
        iv_back!!.visibility = visible
    }

    fun setTitleRightTextSize(size_sp: Int) {
        title_right_btn!!.textSize = size_sp.toFloat()
    }

    fun setBackGroundColor(colorId: Int) {
        titleView!!.setBackgroundColor(resources.getColor(colorId))
    }
}